package com.rtx.edi.platform.application.controller;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.api.enums.FieldValidateType;
import com.rtx.api.utils.FieldValidateUtils;
import com.rtx.edi.model.application.dto.PlApplicationFieldDto;
import com.rtx.edi.model.application.po.PlApplicationField;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.application.service.PlApplicationFieldService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 应用字段信息表  前端控制器
 * </p>
 *
 * @author wq
 * @since 2023-03-16
 */
@Api(tags = {"应用字段管理"})
@RestController
@RequestMapping("/v1/plApplicationField")
@CrossOrigin
public class PlApplicationFieldController {

    @Autowired
    private PlApplicationFieldService plApplicationFieldService;

    /**
     * 获取应用字段列表
     * @return
     */
    @ApiOperation(value = "应用字段列表", notes = "分页获取应用字段列表")
    @PostMapping("selectPlApplicationFieldList")
    public EdiResponse selectPlApplicationFieldList(@RequestBody PlApplicationFieldDto dto){
        PlApplicationField po = new PlApplicationField();
        BeanUtils.copyProperties(dto,po);
        Page<PlApplicationField> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
        plApplicationFieldService.selectPlApplicationFieldList(pageParam,po);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * 获取应用字段
     * @return
     */
    @ApiOperation(value = "获取应用字段", notes = "根据ID获取应用字段")
    @PostMapping("selectPlApplicationFieldById")
    public EdiResponse selectPlApplicationFieldById(@RequestBody PlApplicationFieldDto dto){
        return EdiResponse.success().data("item",plApplicationFieldService.getById(dto.getId()));
    }

    /**
     * 新增应用字段
     * @return
     */
    @ApiOperation(value = "新增应用字段", notes = "新增应用字段")
    @PostMapping("addPlApplicationField")
    public EdiResponse addPlApplicationField(@RequestBody PlApplicationFieldDto dto){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		String companyid = (String) Optional.ofNullable(session.getAttribute("companyid")).orElse("1");
		dto.setCompanyId(Long.parseLong(companyid));
        //验证非空字段
        FieldValidateUtils.fieldNotNull(dto, FieldValidateType.INSERT);
        PlApplicationField po = new PlApplicationField();
        BeanUtils.copyProperties(dto,po);
        return  plApplicationFieldService.addPlApplicationField(po) > 0 ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 修改应用字段
     * @return
     */
    @ApiOperation(value = "修改应用字段", notes = "根据ID修改应用字段")
    @PostMapping("editPlApplicationField")
    public EdiResponse editPlApplicationField(@RequestBody PlApplicationFieldDto dto){
        //验证非空字段
        FieldValidateUtils.fieldNotNull(dto, FieldValidateType.UPDATE);
        PlApplicationField po = new PlApplicationField();
        BeanUtils.copyProperties(dto,po);
        return  plApplicationFieldService.editPlApplicationField(po) > 0 ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 删除应用字段
     * @return
     */
    @ApiOperation(value = "删除应用字段", notes = "根据ID删除应用字段")
    @PostMapping("deletePlApplicationFieldById")
    public EdiResponse deletePlApplicationFieldById(@RequestBody PlApplicationFieldDto dto){
        FieldValidateUtils.fieldNotNull(dto,FieldValidateType.DELETE);
        return  plApplicationFieldService.removeById(dto.getId()) ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 批量删除应用字段
     * @return
     */
    @ApiOperation(value = "批量删除应用字段", notes = "批量删除应用字段")
    @PostMapping("deletePlApplicationFieldByIds")
    public EdiResponse deletePlApplicationFieldByIds(@RequestBody List<PlApplicationFieldDto> list){
        FieldValidateUtils.fieldNotNull(list,FieldValidateType.DELETE);
        List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
        return  plApplicationFieldService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    }
}
